package com.cloudkinto.service.workOrder.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.bo.workOrder.cost.WorkOrderCostAddBo;
import com.cloudkinto.dao.WorkOrderCostDao;
import com.cloudkinto.entity.WorkOrderCostDo;
import com.cloudkinto.service.workOrder.WorkOrderCostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 工单费用项 service实现类
 * </p>
 *
 * @author long
 * @since 2025-01-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkOrderCostServiceImpl extends ServiceImpl<WorkOrderCostDao, WorkOrderCostDo> implements WorkOrderCostService {


    @Override
    public void workOrderAdjustmentFee(WorkOrderCostAddBo req) {
        this.remove(Wrappers.<WorkOrderCostDo>lambdaQuery().eq(WorkOrderCostDo::getWorkOrderId, req.getWorkOrderId()));
        if (CollUtil.isNotEmpty(req.getWorkCostItemList())) {
            List<WorkOrderCostDo> doList = JSONUtil.toList(JSONUtil.toJsonStr(req.getWorkCostItemList()), WorkOrderCostDo.class);
            doList.forEach(doItem -> doItem.setWorkOrderId(req.getWorkOrderId()));
            this.saveBatch(doList);
        }
    }

}
